
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "log.h"                // LOG file 
#define LOG     LOG_INFO0


#include "xapp_clock.h"
#include "xapp_sleep.h"
#include "xapp_localize.h"

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
//static void local_view_event_handler( uint32_t tag );

static uint8_t string_date[11];
static uint8_t string_time[8];
static uint8_t string_sleep_total[8];
static uint8_t string_sleep_deep[8];
static uint8_t string_sleep_light[8];

const static ui_widgets_type_t wglSleepDetail/*1. modify name*/[] = 
{
    // background 
    [0] = uiCanvas( 0, 0, GUI_LCD_RES_H, GUI_LCD_RES_V, COLOR_BLACK ),
    // time 
    [1] = uiNumberlistWithBuffer(SIZE_SCALE(130), SIZE_SCALE(80), SIZE_SCALE(60), SIZE_SCALE(20), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_16_21, "%02d:%02d", string_time, 10),

    // sleep deep icon
    [2] = uiPictureBox( IMG_Sleep_Color_024_024_02, 1, SIZE_SCALE(17), SIZE_SCALE(71), SIZE_SCALE(16), SIZE_SCALE(16) ),
    // sleep light -icon 
    [3] = uiPictureBox( IMG_Sleep_Color_024_024_02, 0, SIZE_SCALE(14), SIZE_SCALE(129), SIZE_SCALE(16), SIZE_SCALE(16) ),

    // total sleep 
    [4] = uiNumberlistWithBuffer( SIZE_SCALE(100), SIZE_SCALE(110), SIZE_SCALE(120), SIZE_SCALE(42), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_32_42, "%02d:%02d", string_sleep_total, 5),
    // deep sleep
    [5] = uiNumberlistWithBuffer( SIZE_SCALE(25), SIZE_SCALE(94) ,SIZE_SCALE(120), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_sleep_deep, 5),
    // light sleep 
    [6] = uiNumberlistWithBuffer( SIZE_SCALE(25), SIZE_SCALE(151), SIZE_SCALE(120), SIZE_SCALE(26), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_20_26, "%02d:%02d", string_sleep_light, 5 ),

    // total sleep percent 212
    [7] = uiCirclebarWithAngle( SIZE_SCALE(160), SIZE_SCALE(120), 0, 0, COLOR_GRAY, SIZE_SCALE(70), SIZE_SCALE(15), 0, 360 ),
    // deep sleep percent 
    [8] = uiCirclebar( SIZE_SCALE(157), SIZE_SCALE(120), 0, 0, 0x3de0, SIZE_SCALE(65), SIZE_SCALE(10) ), 
    // light sleep percent 
    [9] = uiCirclebar( SIZE_SCALE(157), SIZE_SCALE(120), 0, 0, 0xfe05, SIZE_SCALE(65), SIZE_SCALE(10) ),

    // deep sleep text
    [10] = uiTextbox(SIZE_SCALE(35), SIZE_SCALE(67), SIZE_SCALE(60), SIZE_SCALE(29), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
    // light sleep text
    [11] = uiTextbox(SIZE_SCALE(32),SIZE_SCALE(125), SIZE_SCALE(60), SIZE_SCALE(29), COLOR_BLACK, COLOR_WHITE, ALIGN_LEFT_TOP, FILL_NONE, ENCODING_UNICODE, FONT_UNICODE_20_29 ),
    // sleep date 
    [12] = uiNumberlistWithBuffer(SIZE_SCALE(130), SIZE_SCALE(142), SIZE_SCALE(60), SIZE_SCALE(21), COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_1_16_21, "%02d/%02d", string_date, 5)

};



GUI_VIEW_DEF_NR( viewApp_SleepDetail ) = 
{
    // name 
    .name = "App+SleepDetail",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglSleepDetail /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglSleepDetail ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC
};


/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 0;
    menu->lifetime = 5000; 
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
//    p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/
static void display_callback_func(gui_menu_t * menu)
{
    /*
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    
    const xapp_sleep_record_t* sleep = (xapp_sleep_record_t*)xapp_sleep_get();
    if( sleep != NULL ) 
    {
        uint32_t timestamp;
        datetime_t pdt = *xapp_clock_get_now();

        rtc_get_time_last_date( &pdt );
        pdt.hour = 12;
        pdt.minute = 0;
        pdt.second = 0;
        timestamp = rtc_get_timestamp( &pdt );
        
        if( rtc_compare( sleep->timestamp, timestamp ) < 0 ) 
        {
            sleep = NULL; 
        }
    }
    
    if( sleep == NULL )
    {
        prop.numberlist.value[0] = 0; // hour 
        prop.numberlist.value[1] = 0; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[4], &prop );

        // sleep deep 
        prop.numberlist.value[0] = 0; // hour 
        prop.numberlist.value[1] = 0; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[5], &prop );

        // sleep light 
        prop.numberlist.value[0] = 0; // hour 
        prop.numberlist.value[1] = 0; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[6], &prop );

        // 
        widgets[7].visible = 1; 
        widgets[7].uiCirclebar.angles = 0;
        widgets[7].uiCirclebar.anglee = 360;

        // deep percent 
        widgets[8].visible = 0; 
        // light percent 
        widgets[9].visible = 0; 
        
        // time 
        widgets[1].visible = 0;
        // date 
        widgets[12].visible = 0;
    }
    else 
    {
        prop.numberlist.value[0] = sleep->hour;
        prop.numberlist.value[1] = sleep->minute;
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[1], &prop ); 
        
       // update sleep total
        prop.numberlist.value[0] = sleep->TOTAL / 60; // hour 
        prop.numberlist.value[1] = sleep->TOTAL % 60; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[4], &prop );

        // sleep deep 
        prop.numberlist.value[0] = sleep->DEEP / 60; // hour 
        prop.numberlist.value[1] = sleep->DEEP % 60; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[5], &prop );

        // sleep light 
        prop.numberlist.value[0] = sleep->LIGHT / 60; // hour 
        prop.numberlist.value[1] = sleep->LIGHT % 60; // minute 
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[6], &prop );

        widgets[7].visible = 0;
        // widgets[8].visible = 1;
        // widgets[9].visible = 1;

        if( sleep->DEEP == 0)
        {
            widgets[8].visible = 0;
            widgets[9].visible = 1;
            widgets[9].uiCirclebar.angles = 0;
            widgets[9].uiCirclebar.anglee = 360;
        }
        else if( sleep->LIGHT == 0)
        {
            widgets[8].visible = 1;
            widgets[9].visible = 0;
            widgets[8].uiCirclebar.angles = 0;
            widgets[8].uiCirclebar.anglee = 360;
        }
        else 
        {        
            widgets[8].visible = 1;
            widgets[9].visible = 1;

            widgets[8].uiCirclebar.angles = 0; 
            widgets[8].uiCirclebar.anglee = 360 * sleep->DEEP  / (sleep->DEEP + sleep->LIGHT) - 1; 

            widgets[9].uiCirclebar.angles = widgets[8].uiCirclebar.anglee + 2; 
            widgets[9].uiCirclebar.anglee = 360 - 1; //widgets[8].uiCirclebar.anglee + 360 * sleep->lightSleepTime / sleep->total ; 
        }        

        prop.numberlist.value[0] = sleep->month;
        prop.numberlist.value[1] = sleep->date;
        prop.numberlist.cnt = 2;
        iqw_gui_widget_update_prop( &widgets[12], &prop );
        widgets[12].visible = 1;
    }
    // 10 - deep sleep
    string_t* s = (string_t*)xapp_local_text_item_get( LOCAL_TEXT_TYPE_DEEP_SLEEP );
    prop.text.context = (void*)s->text;
    prop.text.len = s->len;
    iqw_gui_widget_update_prop( &widgets[10], &prop );
    // 11 - light sleep 
    s = (string_t*)xapp_local_text_item_get( LOCAL_TEXT_TYPE_LIGHT_SLEEP );
    prop.text.context = (void*)s->text;
    prop.text.len = s->len;
    iqw_gui_widget_update_prop( &widgets[11], &prop );
   */
}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;
    gui_core_lifetime_refresh( menu );
    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DRAG_X:
        if( touch->deltaX > 0 )
        {
            gui_view_create_transition( &viewApp_Sleep, &viewApp_Temperature, &manageTransition, touch->deltaX, 0 );
        }
        else 
        {
            gui_view_create_transition( &viewApp_Sleep, &viewAppsList, &manageTransition, touch->deltaX, 0 );
        }
        break;
       
        case TP_ALGO_EVT_TYPE_SLIDE_D:
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_U:
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_L:
        //gui_view_create_switch( &viewApp_Sleep, &viewAppsList, GUI_SWITCH_TO_LEFT);
        GUI_VIEW_CREATE_NAVIGATE_TO_LEFT( &viewApp_Sleep, &viewAppsList, 0 );
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_R:
        //gui_view_create_switch( &viewApp_Sleep, &viewApp_Bloodpressure, GUI_SWITCH_TO_RIGHT );
        GUI_VIEW_CREATE_NAVIGATE_TO_RIGHT( &viewApp_Sleep, &viewApp_Temperature, 0);
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:
        gui_view_create( views_load_current_watchface() );
        break; 
           
        default:
            break;
    }
 
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{

}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
//static void local_view_event_handler( uint32_t tag )
//{

//}
